package util;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ItemStateListener;

public class PictureItem extends CustomItem {
	Image myImage;

	Image overlayStraight;

	Image overlayBlend;

	Image overlayEdges;

	ItemStateListener parent;

	String lastCommand = "None";

	int mode = 1;

	String[] modes = { "STILL", "LIVE", "STILL OUTLINE", "STILL BLEND" };

	int PLAIN_OVERLAY = 0;

	int PLAIN_VIDEO = 1;

	int OUTLINE_OVERLAY = 2;

	int BLEND_OVERLAY = 3;

	public PictureItem(String title) {
		super("");
		//setLayout(LAYOUT_CENTER);
	}

	public void setItemStateListener(ItemStateListener _il) {
		parent = _il;
	}

	public void refresh() {

		repaint();
	}

	public void setOverlay(Image _img, int _alpha) {

		if (_img != null) {
			overlayStraight = Image.createImage(_img);
			int w = overlayStraight.getWidth();
			int h = overlayStraight.getHeight();
			int[] rgbArrayBlend = new int[w * h];
			int[] rgbArrayEdges = new int[w * h];
			int[] rgbArrayEdgesOutput = new int[w * h];
			overlayStraight.getRGB(rgbArrayBlend, 0, w, 0, 0, w, h);
			overlayStraight.getRGB(rgbArrayEdges, 0, w, 0, 0, w, h);


			for (int row = 1; row < h-1; row++) {
				for (int col = 1; col < w-1; col++) {
					int i = row * w + col;
					int color = (rgbArrayBlend[i] & 0x00FFFFFF); // get the colorof the pixel.
					int a = _alpha; // set the alpha value we want to use 0-255.
					a = (a << 24); // left shift the alpha value 24 bits.
					color += a;
					rgbArrayBlend[i] = color;
					//find edges
					color = (rgbArrayEdges[i] & 0x00FF0000); // get the colorof the pixel.
					int ucolor = (rgbArrayEdges[i-w] & 0x00FF0000); // get the colorof the pixel.
					int pcolor = (rgbArrayEdges[i-1] & 0x00FF0000); // get the colorof the pixel.
					if (Math.abs(color-pcolor) > _alpha/4  ||  Math.abs(color-ucolor) > _alpha/4){
						rgbArrayEdgesOutput[i] = 0xFFFF0000;
					}
					
				}
			}

			overlayBlend = Image.createRGBImage(rgbArrayBlend, w, h, true);
			overlayEdges = Image.createRGBImage(rgbArrayEdgesOutput, w, h, true);
		}
		System.out.println("Create Overlay");

	}

	public void setPicture(Image _i) {
		myImage = _i;

	}

	protected int getMinContentHeight() {
		return 60;
	}

	protected int getMinContentWidth() {
		return 80;
	}

	protected int getPrefContentHeight(int width) {
		return 120;
	}

	protected int getPrefContentWidth(int height) {
		return 140;
	}

	public String getLastCommand() {
		return lastCommand;
	}

	protected void keyPressed(int keyCode) {
		if (keyCode == Canvas.KEY_NUM4) {
			lastCommand = "Left";
			mode = mode + 1;
			if (mode > 3)
				mode = 0;
		} else if (keyCode == Canvas.KEY_NUM6) {
			lastCommand = "Right";
			mode = mode - 1;
			if (mode < 0)
				mode = 3;
		}
		repaint();
	}

	protected void paint(Graphics g, int arg1, int arg2) {
		// TODO Auto-generated method stub
		g.setColor(255);
		g.drawRect(0, 0,g.getClipWidth(),20);
		g.setColor(0);
		g.drawString("Mode:" + modes[mode] + "<4 6>", 0, 0, 0);
		if (myImage != null && mode != 0)
			g.drawImage(myImage, 0, 20, 0);
		if (overlayBlend != null) {
			if (mode == 0)
				g.drawImage(overlayStraight, 0, 20, 0);
			if (mode == 2)
				g.drawImage(overlayEdges, 0, 20, 0);
			if (mode == 3)
				g.drawImage(overlayBlend, 0, 20, 0);

		}
	}
}
